package com.yuzhitong.shapi.presenter;

import com.yuzhitong.shapi.base.BasePresenter;
import com.yuzhitong.shapi.base.net.BaseListBean;
import com.yuzhitong.shapi.base.net.BaseObjectBean;
import com.yuzhitong.shapi.bean.AdStatusBean;
import com.yuzhitong.shapi.bean.CategoryBean;
import com.yuzhitong.shapi.bean.HomeListBean;
import com.yuzhitong.shapi.bean.MovieBean;
import com.yuzhitong.shapi.contract.CategoryContract;
import com.yuzhitong.shapi.contract.HomeContract;
import com.yuzhitong.shapi.contract.MovieContract;
import com.yuzhitong.shapi.model.MainModel;
import com.yuzhitong.shapi.model.MovieModel;
import com.yuzhitong.shapi.net.MyObserver;
import com.yuzhitong.shapi.net.RxScheduler;
import com.yuzhitong.shapi.util.LoggerUtil;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.rxjava3.annotations.NonNull;

/**
 * Created by Brsya
 * CreateDate: 2022/3/24  16:42
 * Description:
 * 首页
 */
public class HomePresenter extends BasePresenter<HomeContract.View> implements HomeContract.Presenter{
    private HomeContract.Model model;

    public HomePresenter() {
        model = new MainModel();
    }

    @Override
    public void getAdState() {
        mView.showLoading();

        Map<String, Object> par = getPublicPar();

        model.getAdState(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<AdStatusBean>>(mView) {
                    @Override
                    public void success(@NonNull BaseListBean<AdStatusBean> baseBean) {
                        baseBean.handleData(AdStatusBean.class);
                        mView.hideLoading();
                        if (baseBean.getData() != null) {
                            for (AdStatusBean datum : baseBean.getData()) {
                                if ("24".equals(datum.getId())) {
//                                    if (datum.getNoState() == 10) {
//                                        mView.openAd();
//                                    }else{
//                                        mView.closeAd();
//                                    }
                                    break;
                                }
                            }
                        }
                    }
                });
    }

    @Override
    public void getSearchUrl(String searchText) {
        mView.showLoading();

        Map<String, Object> par = new HashMap<>();
        if (searchText != null) {
            par.put("value", searchText);
        }
        model.getSearchUrl(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseObjectBean<String>>(mView) {
                    @Override
                    public void success(BaseObjectBean<String> baseBean) {
                        baseBean.handleData(String.class);
                        mView.hideLoading();
                        if (baseBean.getData() != null) {
                            mView.searchUrl(baseBean.getData().replace("\"", ""));
                        } else {
                            mView.searchUrlEmpty();
                        }
                    }
                });


    }

    @Override
    public void getHomeList() {

        mView.showLoading();

        Map<String, Object> par = new HashMap<>();
        model.getHomeList(par)
                .compose(RxScheduler.Obs_io_main())
                .to(mView.bindAutoDispose())
                .subscribe(new MyObserver<BaseListBean<HomeListBean>>(mView) {
                    @Override
                    public void success(BaseListBean<HomeListBean> baseBean) {
                        baseBean.handleData(HomeListBean.class);
                        mView.hideLoading();

                        if (baseBean.getData() == null || baseBean.getData().size() == 0) {
                            return;
                        }

                        mView.showHomeList(baseBean.getData());
                    }
                });
    }
}
